{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "40528f04",
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "from torch import nn"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "08bbda73",
   "metadata": {},
   "outputs": [],
   "source": [
    "conv2d = nn.Conv2d(in_channels=32, \n",
    "                 out_channels=64, \n",
    "                 kernel_size=3, \n",
    "                 stride=2, \n",
    "                 padding=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "3f257b3d",
   "metadata": {},
   "outputs": [],
   "source": [
    "convtrans2d = nn.ConvTranspose2d(in_channels=32, \n",
    "                                 out_channels=64, \n",
    "                                 kernel_size=3, \n",
    "                                 stride=2, \n",
    "                                 padding=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "19b3a0c5",
   "metadata": {},
   "outputs": [],
   "source": [
    "X = torch.randn(4, 32, 128, 128)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "35b96f2d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([4, 64, 64, 64])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "conv2d(X).shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "ea8d9427",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([4, 64, 382, 382])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "convtrans2d(X).shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "3199e736",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "384"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "128 * 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "37bd4ee4",
   "metadata": {},
   "outputs": [],
   "source": [
    "ump = nn.MaxUnpool2d(kernel_size=2, stride=2, padding=0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "13461aea",
   "metadata": {},
   "outputs": [],
   "source": [
    "mp = nn.MaxPool2d(kernel_size=2, stride=2, padding=0, return_indices=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "4c256079",
   "metadata": {},
   "outputs": [],
   "source": [
    "X = torch.randn(1, 1, 4, 4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "3a9c2399",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[[-0.6408, -0.6676,  0.1771,  0.2554],\n",
       "          [-0.2401, -0.3051, -0.5520, -0.9771],\n",
       "          [-0.5377, -1.0093,  0.4614,  0.1394],\n",
       "          [-0.5317,  1.6781,  0.1053,  0.7890]]]])"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "X"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "0d29dc0f",
   "metadata": {},
   "outputs": [],
   "source": [
    "img1, idx = mp(X)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "f6fa65ef",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[[-0.2401,  0.2554],\n",
       "          [ 1.6781,  0.7890]]]])"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "img1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "50a44eeb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[[ 4,  3],\n",
       "          [13, 15]]]])"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "idx"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "ecc2dacb",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[[ 0.0000,  0.0000,  0.0000,  0.2554],\n",
       "          [-0.2401,  0.0000,  0.0000,  0.0000],\n",
       "          [ 0.0000,  0.0000,  0.0000,  0.0000],\n",
       "          [ 0.0000,  1.6781,  0.0000,  0.7890]]]])"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ump(img1, idx)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "id": "f0bb0220",
   "metadata": {},
   "outputs": [],
   "source": [
    "x1 = torch.randn(2, 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "id": "fa5a2d49",
   "metadata": {},
   "outputs": [],
   "source": [
    "x2 = torch.randn(2, 3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "id": "8266fe2d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[-0.9208,  0.0138, -0.1101],\n",
       "        [ 0.6697,  0.2489, -0.5931]])"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "id": "633cd7d7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 0.6093, -0.9586, -0.8933],\n",
       "        [ 1.3282,  0.3443,  0.5182]])"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "id": "00a6c6f1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[-0.9208,  0.0138, -0.1101,  0.6093, -0.9586, -0.8933],\n",
       "        [ 0.6697,  0.2489, -0.5931,  1.3282,  0.3443,  0.5182]])"
      ]
     },
     "execution_count": 53,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.cat(tensors=(x1, x2), dim=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "id": "b63c06e6",
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "  dilation (int or tuple, optional): Spacing between kernel elements. Default: 1\n",
    "\"\"\"\n",
    "conv2d = nn.Conv2d(in_channels=2048, \n",
    "                   out_channels=256,\n",
    "                   kernel_size=3, \n",
    "                   stride=1,\n",
    "                   dilation=36, \n",
    "                   padding=36)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "id": "4c3104aa",
   "metadata": {},
   "outputs": [],
   "source": [
    "X = torch.randn(1, 2048, 60, 60)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "6483a26f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([1, 256, 60, 60])"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "conv2d(X).shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "id": "2be91e80",
   "metadata": {},
   "outputs": [],
   "source": [
    "conv2d = nn.Conv2d(in_channels=3, \n",
    "                   out_channels=12, \n",
    "                   kernel_size=3, \n",
    "                   stride=1, \n",
    "                   padding=1, \n",
    "                   dilation=36)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "id": "7719d729",
   "metadata": {},
   "outputs": [],
   "source": [
    "X = torch.randn(1, 3, 1, 1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "id": "29dd46df",
   "metadata": {},
   "outputs": [
    {
     "ename": "RuntimeError",
     "evalue": "Calculated padded input size per channel: (3 x 3). Kernel size: (73 x 73). Kernel size can't be greater than actual input size",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mRuntimeError\u001b[0m                              Traceback (most recent call last)",
      "\u001b[1;32m~\\AppData\\Local\\Temp\\ipykernel_7956\\4056088892.py\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mconv2d\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mX\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mshape\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32m~\\anaconda3\\lib\\site-packages\\torch\\nn\\modules\\module.py\u001b[0m in \u001b[0;36m_call_impl\u001b[1;34m(self, *input, **kwargs)\u001b[0m\n\u001b[0;32m   1192\u001b[0m         if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks\n\u001b[0;32m   1193\u001b[0m                 or _global_forward_hooks or _global_forward_pre_hooks):\n\u001b[1;32m-> 1194\u001b[1;33m             \u001b[1;32mreturn\u001b[0m \u001b[0mforward_call\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0minput\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m   1195\u001b[0m         \u001b[1;31m# Do not call functions when jit is used\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m   1196\u001b[0m         \u001b[0mfull_backward_hooks\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mnon_full_backward_hooks\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m[\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m~\\anaconda3\\lib\\site-packages\\torch\\nn\\modules\\conv.py\u001b[0m in \u001b[0;36mforward\u001b[1;34m(self, input)\u001b[0m\n\u001b[0;32m    461\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    462\u001b[0m     \u001b[1;32mdef\u001b[0m \u001b[0mforward\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0minput\u001b[0m\u001b[1;33m:\u001b[0m \u001b[0mTensor\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m->\u001b[0m \u001b[0mTensor\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 463\u001b[1;33m         \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_conv_forward\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0minput\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mweight\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mbias\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m    464\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    465\u001b[0m \u001b[1;32mclass\u001b[0m \u001b[0mConv3d\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0m_ConvNd\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;32m~\\anaconda3\\lib\\site-packages\\torch\\nn\\modules\\conv.py\u001b[0m in \u001b[0;36m_conv_forward\u001b[1;34m(self, input, weight, bias)\u001b[0m\n\u001b[0;32m    457\u001b[0m                             \u001b[0mweight\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mbias\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mstride\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m    458\u001b[0m                             _pair(0), self.dilation, self.groups)\n\u001b[1;32m--> 459\u001b[1;33m         return F.conv2d(input, weight, bias, self.stride,\n\u001b[0m\u001b[0;32m    460\u001b[0m                         self.padding, self.dilation, self.groups)\n\u001b[0;32m    461\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mRuntimeError\u001b[0m: Calculated padded input size per channel: (3 x 3). Kernel size: (73 x 73). Kernel size can't be greater than actual input size"
     ]
    }
   ],
   "source": [
    "conv2d(X).shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e2d0a420",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9ceb432d",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1ce6e86f",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
